﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.System.Controllers.Employee
{


    [Admin]
    public class EmployeeController : AdminBaseController
    {
        protected override string navTabId
        {
            get { return "System_Employee"; }
        }

        //
        // GET: /System/Employee/



        public ActionResult Index()
        {
            SearchPara(null);
            InitSelectList();
            return View(GetEmps(null));
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            SearchPara(form);
            InitSelectList();
            return View(GetEmps(form));
        }

        //
        // GET: /System/Employee/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /System/Employee/Create

        public ActionResult Create(long? id)
        {
            TB_SYS_EMP model = null;
            if (id > 0)
            {
                model = serviceEmp.GetByID((long)id);
                model.MODIFYTIME = DateTime.Now;
                model.MODIFYBY = CurentUser.UserName;
            }
            else
            {
                model = new TB_SYS_EMP();
                model.CREATEBY = CurentUser.UserName;
                model.CREATETIME = DateTime.Now;
                model.MODIFYTIME = DateTime.Now;
                model.MODIFYBY = CurentUser.UserName;
            }

            InitSelectList();
            return View(model);
        }

        public ActionResult ReSetPwd(long? id)
        {
            TB_SYS_EMP model = null;
            if (id > 0)
            {
                model = serviceEmp.GetByID((long)id);

            }
            else
            {
                model = new TB_SYS_EMP();

            }

            return View(model);
        }


        //
        // POST: /System/Employee/Create

        [HttpPost]
        public string Create(FormCollection collection)
        {
            string result = string.Empty;
            string tips = "";
            TB_SYS_EMP model = new TB_SYS_EMP();
            TryUpdateModel(model, collection);
            model.ISSALESMAN = Helper.ToInt(Request["ISSALESMAN"], 0);
            model.ISLEAVE = Helper.ToInt(Request["ISLEAVE"], 0);
            model.ISPRINCIPAL = Helper.ToInt(Request["ISPRINCIPAL"], 0);
            model.ISUSER = Helper.ToInt(Request["HID_ISUSER"], -1);
            model.USERTYPE = Helper.ToInt(Request["USERTYPE"], 0);
            model.USERSTATUS = Helper.ToInt(Request["USERSTATUS"], 0);
            model.LOGPWD = Request["HID_LOGPWD"].Trim();


            //验证
            if (model.ISUSER == 1)
            {
                //判断用户名是否为空
                if (model.USERNAME == null || model.USERNAME.Trim() == "")
                {
                    tips += "用户名不能为空。<br/>";
                }

                if (model.ID == 0 && (model.LOGPWD == null || model.LOGPWD.Trim() == ""))
                {
                    tips += "登录密码不能为空。<br/>";
                }

                if (model.USERTYPE > 2 || model.USERTYPE < 1)
                {
                    tips += "请选择用户类型。<br/>";
                }
            }

            if (tips.Trim() == "")
            {
                try
                {


                    int num = model.ID > 0 ? serviceEmp.UpdateEmp(model) : serviceEmp.AddEmp(model);
                    if (num == 1)
                    {

                        //string str = "\"statusCode\":\"{0}\", \"message\":\"{1}\", \"navTabId\":\"{2}\", \"forwardUrl\":\"{3}\", \"callbackType\":\"{4}\"";

                        // System/Employee/index
                        //string.Format(str, 200, "保存成功", "Employee", "System/Employee/index", "closeCurrent");

                        result = AjaxResultSucess;
                    }
                    else if (num == -1)
                    {
                        result = string.Format(HelperCommon.AJAXFORMAT, 300, "用户名已经存在。", navTabId, "", "");
                    }
                    else if (num == -2)
                    {
                        result = string.Format(HelperCommon.AJAXFORMAT, 300, "登录密码不能为空。", navTabId, "", "");
                    }
                    else
                    { result = AjaxResultFail; }
                }
                catch (Exception ex)
                {
                    base.ExceptionHand(ex);
                    result = AjaxResultFail;
                }
            }
            else
            {
                result = string.Format(HelperCommon.AJAXFORMAT, 300, tips, navTabId, "", "");
            }

            return "{" + result + "}";
        }

        //
        // GET: /System/Employee/Edit/5

        public ActionResult EditRoels(long id)
        {
            ViewData["EMPID"] = id;
            var userRoles = serviceSysUserPriv.GetList(new TB_SYS_USERPRIV() { EMPID = id });
            userRoles = userRoles ?? new List<TB_SYS_USERPRIV>();
            ViewData["Roles"] = serviceRoles.GetAll() ?? new List<GSLMS.Entity.Role>();
            return View(userRoles);
        }

        //
        // POST: /System/Employee/Edit/5

        [HttpPost]
        public string EditRoels(FormCollection collection)
        {
            string result = string.Empty;
            try
            {
                var userRoles = new List<TB_SYS_USERPRIV>();
                TryUpdateModel(userRoles, collection);

                bool isSumbit = serviceSysUserPriv.Add(userRoles);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        //
        // GET: /System/Employee/Delete/5
        //public ActionResult Delete()
        //{
        //    string result = string.Empty;


        //    //try
        //    //{


        //    //    bool isSumbit =serviceEmp.Delete(id);
        //    //    if (isSumbit)
        //    //    { result = AjaxResultSucess; }
        //    //    else
        //    //    { result = AjaxResultFail; }
        //    //}
        //    //catch (Exception ex)
        //    //{
        //    //    base.ExceptionHand(ex);
        //    //    result = AjaxResultFail;
        //    //}

        //    return "{" + result + "}";
        //}

        //
        // POST: /System/Employee/Delete/5

        [HttpPost]
        public string Delete(long id, FormCollection collection)
        {
            string result = string.Empty;
            try
            {
                bool isSumbit = serviceEmp.Delete(id);
                if (isSumbit)
                {
                    result = AjaxDeleteSucess;
                }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxDeleteFail;
            }

            return "{" + result + "}";
        }

        [HttpPost]
        public string LockUser(long id, FormCollection collection)
        {
            string result = string.Empty;

            bool isSumbit = serviceEmp.LockOrUnLockUser(id, 0);
            if (isSumbit)
            {
                result = AjaxActionSucess(ResourceManager.ActionSuccess);
            }
            else
            { result = AjaxActionFail(ResourceManager.ActionFail); }

            return "{" + result + "}";
        }

        [HttpPost]
        public string UnLockUser(long id, FormCollection collection)
        {
            string result = string.Empty;
            bool isSumbit = serviceEmp.LockOrUnLockUser(id, 1);
            if (isSumbit)
            {
                result = AjaxActionSucess(ResourceManager.ActionSuccess);
            }
            else
            { result = AjaxActionFail(ResourceManager.ActionFail); }

            return "{" + result + "}";
        }

        [HttpPost]
        public string ReSetPwd(FormCollection collection)
        {
            string result = string.Empty;
            string userName = "";
            string pwd = "";
            long id = Helper.ToLong(Request["ID"].Trim());

            userName = Request["USERNAME"].Trim();
            pwd = Request["HID_LOGPWD"].Trim();

            if (userName == "")
            {
                result = AjaxActionFail(ResourceManager.UnOperateUserPwd);
            }
            else
            {
                int num = serviceEmp.ResetPwd(id, pwd);
                if (num == 1)
                    result = AjaxResultSucess;
                else
                    result = AjaxResultFail;
            }



            return "{" + result + "}";
        }

        private List<TB_SYS_EMP> GetEmps(FormCollection form)
        {
            var emps = new List<TB_SYS_EMP>();
            var searchParma = new TB_SYS_EMP();
            if (ViewData["SearchParma"] == null)
            {
                if (form == null || form.Count == 0)
                {
                    searchParma.ISHOLIDAYS = -1;
                    searchParma.ISLEAVE = -1;
                    searchParma.ISUSER = -1;
                    searchParma.ORGID = 0;
                    searchParma.USERTYPE = -1;
                    searchParma.USERSTATUS = -1;
                    searchParma.Pagination = new Entity.DTO.PaginationDTO();
                    searchParma.Pagination.ReCount = true;
                    searchParma.Pagination.PageSize = Helper.PageSize;
                    searchParma.Pagination.IsPaging = true;
                    searchParma.Pagination.CurrentIndex = 1;

                }
                else
                {
                    base.SearchPara<TB_SYS_EMP>(form, searchParma);
                    //TryUpdateModel(searchParma, form);
                }
            }
            else
            {
                base.SearchPara<TB_SYS_EMP>(form, searchParma);
                //searchParma = ViewData["SearchParma"] as GSLMS.Entity.TB_SYS_EMP ?? new GSLMS.Entity.TB_SYS_EMP();
                
            }

            emps = serviceEmp.GetList(searchParma);
            emps = emps ?? new List<TB_SYS_EMP>();
            ViewData["RowCount"] = searchParma.RowCount;
            return emps;
        }
        private TB_SYS_EMP SearchPara(FormCollection form)
        {
            var searchParma = new TB_SYS_EMP();
            if (form != null && form.Count > 0)
            {
                TryUpdateModel(searchParma, form);
            }
            ViewData["SearchParma"] = searchParma ?? new TB_SYS_EMP();
            return searchParma;
        }

        private void InitSelectList()
        {
            //ViewData["SalesMan"] = GetListFromDict("SalesMan");
            //ViewData["RegStatus"] = EnumHelper.ToSelectList<ApplyEnum.RegStatus>();
            //ViewData["Source"] = EnumHelper.ToSelectList<ApplyEnum.RegSource>();
            //ViewData["Mediator"] = new SelectList(new List<TB_REST_CHANNEL>());
            //ViewData["BussType"] = EnumHelper.ToSelectList<ApplyEnum.RegBussType>();
            //ViewData["Product"] = GetListFromDict("Product");
            //ViewData["ImportantLevel"] = GetListFromDict("ImportantLevel");


            ViewData["OrgList"] = new SelectList(serviceSysOrg.GetAllList(), "id", "orgname");
            ViewData["ISHOLIDAYS"] = EnumHelper.ToSelectList<SystemEnum.ISHOLIDAYS>();
            ViewData["ISLEAVE"] = EnumHelper.ToSelectList<SystemEnum.ISLEAVE>();
            ViewData["ISUSER"] = EnumHelper.ToSelectList<SystemEnum.ISUSER>();
            ViewData["USERTYPE"] = EnumHelper.ToSelectList<SystemEnum.USERTYPE>();
            ViewData["USERSTATUS"] = EnumHelper.ToSelectList<SystemEnum.USERSTATUS>();


        }

       
        public ActionResult Selector(FormCollection form)
        {
            var emps = new List<TB_SYS_EMP>();
            var searchParma = new TB_SYS_EMP();
            if (ViewData["SearchParma"] == null)
            {
                if (form == null || form.Count == 0)
                {
                    searchParma.ISHOLIDAYS = -1;
                    searchParma.ISLEAVE = -1;
                    searchParma.ISUSER = -1;
                    searchParma.ORGID = 0;
                    searchParma.USERTYPE = -1;
                    searchParma.USERSTATUS = -1;
                }
                else
                {
                    TryUpdateModel(searchParma, form);
                }
            }
            else
                searchParma = ViewData["SearchParma"] as GSLMS.Entity.TB_SYS_EMP ?? new GSLMS.Entity.TB_SYS_EMP();

            emps = serviceEmp.GetList(searchParma);
            emps = emps ?? new List<TB_SYS_EMP>();
            ViewData["RowCount"] = searchParma.RowCount;
            return View(emps);
        }

        public ActionResult SingleSelector(FormCollection form)
        {
            var emps = new List<TB_SYS_EMP>();
            var searchParma = new TB_SYS_EMP();
            if (ViewData["SearchParma"] == null)
            {
                if (form == null || form.Count == 0)
                {
                    searchParma.ISHOLIDAYS = -1;
                    searchParma.ISLEAVE = -1;
                    searchParma.ISUSER = -1;
                    searchParma.ORGID = 0;
                    searchParma.USERTYPE = -1;
                    searchParma.USERSTATUS = -1;
                }
                else
                {
                    TryUpdateModel(searchParma, form);
                }
            }
            else
                searchParma = ViewData["SearchParma"] as GSLMS.Entity.TB_SYS_EMP ?? new GSLMS.Entity.TB_SYS_EMP();

            emps = serviceEmp.GetList(searchParma);
            emps = emps ?? new List<TB_SYS_EMP>();
            ViewData["RowCount"] = searchParma.RowCount;
            return View(emps);
        }
    }
}
